/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pc.controller.ware;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.po.WareCategoryPo;
import com.itttq.gshop.domain.po.WareInfoPo;
import com.itttq.gshop.generic.connector.IUserCartConnector;
import com.itttq.gshop.generic.connector.IWareCatBrandConnector;
import com.itttq.gshop.generic.connector.IWareInfoConnector;
import com.itttq.gshop.generic.util.Constants;
import com.itttq.gshop.generic.util.Constants.WareStatus;
import com.itttq.gshop.pc.controller.GenericPcController;
import com.itttq.gshop.pc.controller.IndexController;

@RestController
@RequestMapping("ware")
public class WareInfoController extends GenericPcController {
	private static final long serialVersionUID = -7435776176064932L;
	public static final String cacheKey = "ware";
	@Autowired
	private IWareInfoConnector wareInfoConnector;
	@Autowired
	private IWareCatBrandConnector wareCatBrandConnector;
	@Autowired
	private IUserCartConnector userCartConnector;
	/**
	 * 商品列表
	 * 
	 * @param catid	商品栏目ID
	 * @param tid		商品分类ID
	 * @param classid	商品子类ID
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("list/{catid}.html")
    public ModelAndView getWares(@PathVariable("catid")Integer catid, WareInfoPo ware, HttpServletRequest request) {
        return getWares(catid, null, ware, request);
    }
	/**
	 * 商品列表
	 * 
	 * @param catid	商品栏目ID
	 * @param tid		商品分类ID
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("list/{catid}/{tid}.html")
    public ModelAndView getWares(@PathVariable("catid")Integer catid, @PathVariable("tid")Integer tid,
    		WareInfoPo ware, HttpServletRequest request) {
        return getWares(catid, tid, null, ware, request);
    }
	/**
	 * 商品列表
	 * 
	 * @param catid	商品栏目ID
	 * @param tid		商品分类ID
	 * @param classid	商品子类ID
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("list/{catid}/{tid}/{classid}.html")
    public ModelAndView getWares(@PathVariable("catid")Integer catid, @PathVariable("tid")Integer tid, 
    		@PathVariable("classid")Integer classid, WareInfoPo ware, HttpServletRequest request) {
        return getWares(catid, tid, classid, null, ware, request);
    }
	
	/**
	 * 商品列表
	 * 
	 * @param catid	商品栏目ID
	 * @param tid		商品分类ID
	 * @param classid	商品子类ID
	 * @param bid		品牌ID
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("list/{catid}/{tid}/{classid}/{bid}.html")
    public ModelAndView getWares(@PathVariable("catid")Integer catid, @PathVariable("tid")Integer tid, @PathVariable("classid")Integer classid, 
    		@PathVariable("bid")Integer bid, WareInfoPo ware, HttpServletRequest request) {
		List<WareCategoryPo> cats = 	null;
//		Map<String, List<CatBrandTo>> catBrands = wareCatBrandConnector.getAllEnableCatBrands();
//		if(catBrands.isEmpty()) return mv;
		if(null != catid && catid > 0) {
			ware.setCategoryId(catid);
			cats = IndexController.catsMap.get(String.valueOf(catid));
			if(null != cats && !cats.isEmpty())ware.setCategoryName(cats.get(0).getParentName());
		}
		ware.setStatus(WareStatus.ONSHELF.getVal());
		ware.setSize(12);
		if(null != tid && tid > 0) {
			ware.setTypeId(tid);
			cats = IndexController.catsMap.get(String.valueOf(tid));
			if(null != cats && !cats.isEmpty())ware.setTypeName(cats.get(0).getParentName());
		}
		if(null != classid && classid > 0) {
			ware.setClassId(classid);
			cats = IndexController.catsMap.get(String.valueOf(classid));
			if(null != cats && !cats.isEmpty())ware.setClassName(cats.get(0).getParentName());
		}
		if(null != bid && bid > 0) ware.setBrandId(bid);
        return getWares(ware, request);
    }
	
	/**
	 * 商品列表
	 * 
	 * @param catid	商品栏目ID
	 * @param tid		商品分类ID
	 * @param classid	商品子类ID
	 * @param bid		品牌ID
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("list.html")
    public ModelAndView getWares(WareInfoPo ware, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/ware/wares");
		this.setPpt(request, ware);
		ware.setSize(12);
		// 获取本大类推荐商品
		WareInfoPo wareInfo = new WareInfoPo();
		wareInfo.setStatus(WareStatus.ONSHELF.getVal());
		wareInfo.setRecommend(Constants.STATUTS_OPEN);
		wareInfo.setSize(10);
		// 如果是有分类则获取大类的推荐商品
		if(null != ware && null != ware.getCategoryId()) {
			wareInfo.setCategoryId(ware.getCategoryId());
		}
		mv.addObject("recommends", wareInfoConnector.getWareInfos(wareInfo).getData());
		// 搜索商品
		this.getPageMv("wares", mv, wareInfoConnector.getWareInfos(ware));
		mv.addObject("ware", ware);
		
		mv.addObject("cats", getCats());
		getCommon(request, mv);
        return mv;
    }
	
	@RequestMapping("/{id}.html")
	public ModelAndView getWareInfo(@PathVariable("id") int id, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/ware/ware");
		mv.addObject("ware", wareInfoConnector.getWareInfo(id));
		//TODO GXG  获取推荐商品
		WareInfoPo ware = new WareInfoPo();
		ware.setStatus(WareStatus.ONSHELF.getVal());
		ware.setRecommend(Constants.STATUTS_OPEN);
		ware.setSize(10);
		// 获取本大类推荐商品
		mv.addObject("recommends", wareInfoConnector.getWareInfos(ware).getData());

		mv.addObject("cats", getCats());
		getCommon(request, mv);
		return mv;
	}

}